package cn.tedu.order.demo;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class Order {

    //自动装配spring加密对象
    private static BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();


    public static void main(String[] args) {

        //自动生成随机订单号的方法
        String orderCode = orderCode();
        System.out.println("随机生成的订单号为  " + orderCode);

        //调用加密订单的方法
        String encodeOrder = encodeOrder(orderCode);
        System.out.println("加密后的订单号为 = " + encodeOrder);

        //对比存储的加密订单的方法
        matchesOrder(orderCode,encodeOrder);

    }
    //生成随机订单号的方法
    public static String orderCode(){


        String s = UUID.randomUUID().toString().replace("-","");
        return s;
    }

    //简易  加密订单的方法
    public static String encodeOrder(String orderCode) {

        String encodedOrder =null;
        for (int i = 0; i < 10; i++) {
            String rawOrder = orderCode;
            //调用spring 加密方法
             encodedOrder = passwordEncoder.encode(rawOrder);
        }
        return encodedOrder;
    }

        //对比订单号的方法           //原订单号         //加密后的订单号
    public static void matchesOrder(String rawOrder,String encodedOrder) {

        boolean matchResult = passwordEncoder.matches(rawOrder, encodedOrder);
        System.out.println("match result : " + matchResult);
    }
}
